package com.sample.wanandroidx.app

import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.Intent
import androidx.multidex.MultiDex
import com.sample.wanandroidx.data.User
import com.sample.wanandroidx.ui.home.StartActivity
import kotlin.properties.Delegates

class App : Application() {

    private var lifecycleCallback: AQActivityLifecycleCallbacks? = null

    companion object {
        var user: User? = null

        private var instance: App by Delegates.notNull()
        fun instance() = instance
    }

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
        lifecycleCallback = AQActivityLifecycleCallbacks()

        registerActivityLifecycleCallbacks(lifecycleCallback)
    }

    fun getCurrentVisibleActivity(): Activity? {
        return lifecycleCallback?.currentVisibleActivity
    }

    fun isInForeground(): Boolean? {
        return lifecycleCallback?.isInForeground
    }

    fun finishAllActivityAndStartLogin() {
        lifecycleCallback?.finishAll()
        val intent = Intent(this, StartActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        startActivity(intent)
    }

}